package practice_3_4;

import java.util.Arrays;

//调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
//如数组：[1,2,3,4,5,6]
//调整后可能是：[1, 5, 3, 4, 2, 6]
public class OddPrecedeEven {
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,6};
        System.out.println(Arrays.toString(arr));
        oddPrecedeEven(arr);
        System.out.println(Arrays.toString(arr));
        String s = null;
        System.out.println("s = "+ s);
    }

    public static void oddPrecedeEven(int[] nums){
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            //从左往右找偶数
            while(left < right && nums[left] % 2 == 1){
                left++;
            }
            //从右往左找奇数
            while(left < right && nums[right] % 2 == 0){
                right--;
            }
            //找到进行交换位置
            if(left < right){
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
                left++;
                right--;
            }
        }
    }
}
